import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';

String assetPath(String name, {String prefix = "drawable/"}) {
  return "$prefix$name";
}

String getErrPath({String errName = "img_def_load_fail_pic.png"}) {
  return "${assetPath(errName)}";
}

assetImage(String src,{Key? key,
  double scale = 1.0,
  ImageFrameBuilder? frameBuilder,
  ImageLoadingBuilder? loadingBuilder,
  ImageErrorWidgetBuilder? errorBuilder,
  String? semanticLabel,
  bool excludeFromSemantics = false,
  double? width,
  double? height,
  Color? color,
  BlendMode? colorBlendMode,
  BoxFit? fit,
  AlignmentGeometry alignment = Alignment.center,
  ImageRepeat repeat = ImageRepeat.noRepeat,
  Rect? centerSlice,
  bool matchTextDirection = false,
  bool gaplessPlayback = false,
  FilterQuality filterQuality = FilterQuality.low,
  bool isAntiAlias = false,
  Map<String, String>? headers,
  int? cacheWidth,
  int? cacheHeight,
}) {

  // if(frameBuilder==null){
  //   frameBuilder=(BuildContext context, Widget child, int? frame,
  //       bool wasSynchronouslyLoaded){
  //     return Image.asset(getErrPath(),width: width,height: height);
  //   };
  // }
  // if(loadingBuilder==null){
  //   loadingBuilder=(BuildContext context, Widget child,
  //       ImageChunkEvent? loadingProgress){
  //     return Image.asset(getErrPath(),width: width,height: height,);
  //   };
  // }
  if(errorBuilder==null){
    errorBuilder=(
        BuildContext context,
        Object error,
        StackTrace? stackTrace,
        ){
      return Image.asset(getErrPath(),width: width,height: height,fit: BoxFit.fill,);
    };
  }
  return Image.network(src,
    key: key,
    scale: scale,
    frameBuilder: frameBuilder,
    loadingBuilder: loadingBuilder,
    errorBuilder: errorBuilder,
    semanticLabel: semanticLabel,
    excludeFromSemantics: excludeFromSemantics,
    width: width,
    height: height,
    color: color,
    colorBlendMode: colorBlendMode,
    fit: fit,
    alignment:alignment,
    repeat:repeat,
    centerSlice:centerSlice,
    matchTextDirection:matchTextDirection,
    gaplessPlayback:gaplessPlayback,
    filterQuality:filterQuality,
    isAntiAlias:isAntiAlias,
    headers:headers,
    cacheWidth:cacheWidth,
    cacheHeight:cacheHeight,
  );
}

assetFadeInImage({Key? key,
  String? placeholder,
  ImageErrorWidgetBuilder? placeholderErrorBuilder,
  required String image,
  ImageErrorWidgetBuilder? imageErrorBuilder,
  AssetBundle? bundle,
  double? placeholderScale,
  double imageScale = 1.0,
  bool excludeFromSemantics = false,
  String? imageSemanticLabel,
  Duration fadeOutDuration = const Duration(milliseconds: 300),
  Cubic fadeOutCurve = Curves.easeOut,
  Duration fadeInDuration = const Duration(milliseconds: 700),
  Cubic fadeInCurve = Curves.easeIn,
  double? width,
  double? height,
  BoxFit? fit,
  Alignment alignment = Alignment.center,
  ImageRepeat repeat = ImageRepeat.noRepeat,
  bool matchTextDirection = false,
  int? placeholderCacheWidth,
  int? placeholderCacheHeight,
  int? imageCacheWidth,
  int? imageCacheHeight
}) {
  if(placeholderErrorBuilder==null){
    placeholderErrorBuilder=(_, error, stackTrace){
      return Image.asset(getErrPath(),width: width,height: height);
    };
  }
  if(imageErrorBuilder==null){
    imageErrorBuilder=(_, error, stackTrace){
      print("image_path   圖片異常");
      return Image.asset(getErrPath(),width: width,height: height,);
    };
  }
  if(placeholder==null) placeholder=getErrPath();
  if(placeholderCacheWidth==null && width!=null && width!=double.infinity) placeholderCacheWidth=width.toInt();
  if(placeholderCacheWidth==null && height!=null && height!=double.infinity) placeholderCacheHeight=height.toInt();
  if(fit==null) fit=BoxFit.fill;
  print("image_path   "+image);
  return FadeInImage.assetNetwork(key:key,placeholder: placeholder,
      placeholderErrorBuilder: placeholderErrorBuilder,
      image:image+"",
      imageErrorBuilder:imageErrorBuilder,
      bundle:bundle,
      placeholderScale:placeholderScale,
      imageScale:imageScale,
      excludeFromSemantics:excludeFromSemantics,
      imageSemanticLabel:imageSemanticLabel,
      fadeOutDuration:fadeOutDuration,
      fadeOutCurve:fadeOutCurve,
      fadeInDuration:fadeInDuration,
      fadeInCurve:fadeInCurve,
      width:width,
      height:height,
      fit:fit,
      alignment:alignment,
      repeat:repeat,
      matchTextDirection:matchTextDirection,
      placeholderCacheWidth:placeholderCacheWidth,
      placeholderCacheHeight:placeholderCacheHeight,
      imageCacheWidth:imageCacheWidth,
      imageCacheHeight:imageCacheHeight
  );
}
